package com.android.httpdnsdemo;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.sdk.android.httpdns.HttpDns;
import com.alibaba.sdk.android.httpdns.HttpDnsService;

import java.util.ArrayList;
import java.util.Arrays;

public class SplashActivity extends AppCompatActivity {
    private TextView tv_jump;
    private int time = 5;
    private static HttpDnsService httpdns;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                time--;
                if (time < 0) {
                    toWebviewActivity();
                } else {
                    tv_jump.setText("跳过（" + time + "秒）");
                    handler.sendEmptyMessageDelayed(1, 1000);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        tv_jump = findViewById(R.id.tv_jump);
        tv_jump.setText("跳过（" + time + "秒）");
        handler.sendEmptyMessageDelayed(1, 1000);


    }

    private void initHttpDns() {
        // 初始化httpdns
        httpdns = HttpDns.getService(getApplicationContext(), "149345");
        httpdns.setPreResolveHosts(new ArrayList<>(Arrays.asList("www.apple.com", "www.hellorf.com","lanhuapp.com")));

        // 允许过期IP以实现懒加载策略
        httpdns.setExpiredIPEnabled(true);
    }

    public void onJump(View view) {
        toWebviewActivity();
    }

    private void toWebviewActivity() {
        handler.removeCallbacksAndMessages(null);
        startActivity(new Intent(this, WebviewActivity.class));
        finish();
    }
}
